package com.ojoin.trade.common_core.web.dao.impl;

import java.util.HashMap;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.common_core.web.dao.TbMerchantUserDao;
import com.ojoin.trade.common_core.web.domain.TbMerchantUser;
import com.ojoin.trade.common_core.web.domain.TbMerchantUserBusinessType;

@Repository
public class TbMerchantUserDaoImpl extends BaseDaoImpl<TbMerchantUser> implements TbMerchantUserDao {
	public SqlSession getSqlSession() {
		return this.sqlSessionTemplate;
	}

	@Override
	public TbMerchantUser queryUserByPaynow(String mchno)
			throws BusinessException {
		String mapMethodName = "selectBypaynow";
		try {
			return sqlSessionTemplate.selectOne(
					getSqlName(mapMethodName),
					mchno);
		} catch (Exception e) {
			throw new DaoException(String.format("查询对象列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public int updateMhtInfoById(String userIdOfIPayNow, String mhtname, String mobile)
			throws BusinessException {
		String mapMethodName = "updateMhtInfoById";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("userIdOfIPayNow", userIdOfIPayNow);
			paramMap.put("mhtname", mhtname);
			paramMap.put("mobile", mobile);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新商户的商户名和手机号等信息出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public String selectTsByID(int id, String tablename)
			throws BusinessException {
		String mapMethodName = "selectTsByID";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("id", id);
			paramMap.put("tablename", tablename);
			
			return getSqlSession().selectOne(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("查询tablename->" + tablename + ", id->" + id +"的ts字段值出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

    @Override
    public TbMerchantUser queryUserByCompany(int id) throws BusinessException {
        String mapMethodName = "selectByCompanyid";
        try {
            return sqlSessionTemplate.selectOne(
                    getSqlName(mapMethodName),
                    id);
        } catch (Exception e) {
            throw new DaoException(String.format("查询对象列表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

	@Override
	public TbMerchantUserBusinessType queryBusinessTypeByJoinuserId(int joinuserid)
			throws BusinessException {
		String mapMethodName = "queryBusinessTypeByJoinuserId";
        try {
            return sqlSessionTemplate.selectOne(
                    getSqlName(mapMethodName),
                    joinuserid);
        } catch (Exception e) {
            throw new DaoException(String.format("查询九盈用户已开通业务出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
	}

	public int updateShortnameById(int joinuserid, String shortname) throws BusinessException {
		String mapMethodName = "updateShortnameById";
        try {
        	HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("joinuserid", joinuserid);
			paramMap.put("shortname", shortname);
			
			return sqlSessionTemplate.update(
                    getSqlName(mapMethodName),
                    paramMap);
        } catch (Exception e) {
            throw new DaoException(String.format("更新九盈用户简称出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
	}

	@Override
	public String queryNameByLoginNo(String loginNo) throws BusinessException {
		 String mapMethodName = "queryNameByLoginNo";
        try {
            return sqlSessionTemplate.selectOne(getSqlName(mapMethodName),loginNo);
        } catch (Exception e) {
            throw new BusinessException(String.format("查询对象列表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
	}
	
}
